package summer.window;

import java.util.HashMap;
import java.util.Map;

public class Solution424 {
    public int characterReplacement(String s, int k) {
        int[] hash = new int[26];
        int i = 0, j = 0;
        int res = 0;
        while (i < s.length()) {
            int cur = s.charAt(i) - 'A';
            hash[cur]++;
            while (!check(hash, k)) {
                int del = s.charAt(j) - 'A';
                hash[del]--;
                j++;
            }
            res = Math.max(res, i - j + 1);
            i++;
        }
        return res;
    }

    public boolean check(int[] hash, int k) {
        int sum = 0;
        int max = 0;
        for (int x : hash) {
            sum += x;
            max = Math.max(max, x);
        }
        return sum - max <= k;
    }
}
